package org.liaohailong.helloworld;

import org.junit.Test;
import org.liaohailong.helloworld.practicemiddle.ArrayStringPractice;
import org.liaohailong.helloworld.practicemiddle.LinkedListPractice;
import org.liaohailong.helloworld.practicemiddle.RecallPractice;

import java.util.Arrays;
import java.util.List;

/**
 * Author: liaohailong
 * Time: 2021/5/12 09:46
 * Describe:
 */
public class PracticeMiddle implements AUtil.ILog {
    @Test
    public void threeSum() {
        ArrayStringPractice asp = new ArrayStringPractice();
//        int[] arr = new int[]{-1, 0, 1, 2, -1, -4};
        int[] arr = new int[]{-1, 0, 1, 2, -1, -4, -2, -3, 3, 0, 4};
        List<List<Integer>> lists = asp.threeSum(arr);
        for (List<Integer> list : lists) {
            log(list.toString());
        }
    }

    @Test
    public void lengthOfLongestSubstring() {
        ArrayStringPractice asp = new ArrayStringPractice();
//        String s = "abcabcbb";
//        String s = "";
//        String s = "bbbbb";
//        String s = "pwwkew";
        String s = "au";
        int i = asp.lengthOfLongestSubstring(s);
        log("i = " + i);
    }

    @Test
    public void setZeroes() {
        ArrayStringPractice asp = new ArrayStringPractice();
        int[][] matrix = new int[][]{{0, 1, 2, 0}, {3, 4, 5, 2}, {1, 3, 1, 5}};
        asp.setZeroes(matrix);
        log(Arrays.deepToString(matrix));
    }

    @Test
    public void groupAnagrams() {
        ArrayStringPractice asp = new ArrayStringPractice();
        String[] str = new String[]{"eat", "tea", "tan", "ate", "nat", "bat"};
        List<List<String>> lists = asp.groupAnagrams(str);
        log(lists.toString());
    }

    @Test
    public void longestPalindrome() {
        ArrayStringPractice asp = new ArrayStringPractice();
//        String s = "babad";
//        String s = "cbbd";
//        String s = "a";
//        String s = "ac";
        String s = "abcddddd";
//        String s = "";
        String res = asp.longestPalindrome(s);
        log(res);
    }

    @Test
    public void increasingTriplet() {
        ArrayStringPractice asp = new ArrayStringPractice();
//        int[] nums = new int[]{1, 2, 3, 4, 5};
//        int[] nums = new int[]{5, 4, 3, 2, 1};
//        int[] nums = new int[]{2, 1, 5, 0, 4, 6};
        int[] nums = new int[]{20, 100, 10, 12, 5, 13};
//        int[] nums = new int[]{1, 5, 0, 4, 1, 3};
//        int[] nums = new int[]{0, 4, 2, 1, 0, -1, -3};
        boolean b = asp.increasingTriplet(nums);
        log("b = " + b);
    }

    @Test
    public void addTwoNumbers() {
        LinkedListPractice llp = new LinkedListPractice();
        LinkedListPractice.ListNode l1 = llp.create(new int[]{2, 4, 3});
        LinkedListPractice.ListNode l2 = llp.create(new int[]{5, 6, 4});

//        LinkedListPractice.ListNode l1 = llp.create(new int[]{0});
//        LinkedListPractice.ListNode l2 = llp.create(new int[]{0});

//        LinkedListPractice.ListNode l1 = llp.create(new int[]{9, 9, 9, 9, 9, 9, 9});
//        LinkedListPractice.ListNode l2 = llp.create(new int[]{9, 9, 9, 9});

        LinkedListPractice.ListNode res = llp.addTwoNumbers(l1, l2);
        llp.print(res);
    }

    @Test
    public void oddEvenList() {
        LinkedListPractice llp = new LinkedListPractice();
//        LinkedListPractice.ListNode head = llp.create(new int[]{1, 2, 3, 4, 5});
        LinkedListPractice.ListNode head = llp.create(new int[]{2, 1, 3, 5, 6, 4, 7});
        LinkedListPractice.ListNode list = llp.oddEvenList(head);
        llp.print(list);
    }

    @Test
    public void getIntersectionNode() {
        LinkedListPractice llp = new LinkedListPractice();
        LinkedListPractice.ListNode tail = llp.create(new int[]{2, 4});
        LinkedListPractice.ListNode headA = llp.create(new int[]{1, 9, 1});
        LinkedListPractice.ListNode headB = llp.create(new int[]{3});
        llp.tail(headA).next = tail;
        llp.tail(headB).next = tail;
        LinkedListPractice.ListNode node = llp.getIntersectionNode(headA, headB);
        llp.print(node);
    }

    @Test
    public void letterCombinations() {
//        输入：digits = "23"
//        输出：["ad","ae","af","bd","be","bf","cd","ce","cf"]
        RecallPractice rp = new RecallPractice();
        String digits = "23";
//        String digits = "";
//        String digits = "2";
        List<String> result = rp.letterCombinations(digits);
        log(result.toString());
    }

    @Test
    public void generateParenthesis() {
        RecallPractice rp = new RecallPractice();
//        int N = 3;
        int N = 1;
//        int N = 3;
        List<String> result = rp.generateParenthesis(N);
        log(result.toString());
    }

    @Test
    public void permute() {
        RecallPractice rp = new RecallPractice();
//        int[] nums = {1, 2, 3};
//        int[] nums = {0,1};
        int[] nums = {1};
        List<List<Integer>> permute = rp.permute(nums);
        log(permute.toString());
    }

    @Test
    public void subsets() {
        RecallPractice rp = new RecallPractice();
        int[] nums = {1, 2, 3};
//        int[] nums = {1, 2, 3, 4, 5, 6, 7, 8, 10, 0};
        List<List<Integer>> subsets = rp.subsets(nums);
        log(subsets.toString());
    }

    @Test
    public void exist() {
        AUtil.setLog(this);
//        输入：board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCCED"
//        输出：true
//        char[][] board = {
//                {'A', 'B', 'C', 'E'},
//                {'S', 'F', 'C', 'S'},
//                {'A', 'D', 'E', 'E'},
//        };
//        String word = "ABCCED";

//        输入：board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "SEE"
//        输出：true
//        char[][] board = {
//                {'A', 'B', 'C', 'E'},
//                {'S', 'F', 'C', 'S'},
//                {'A', 'D', 'E', 'E'},
//        };
//        String word = "SEE";

//        输入：board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCB"
//        输出：false
//        char[][] board = {
//                {'A', 'B', 'C', 'E'},
//                {'S', 'F', 'C', 'S'},
//                {'A', 'D', 'E', 'E'},
//        };
//        String word = "ABCB";

//        [["A","A","A","A","A","A"],
//        ["A","A","A","A","A","A"],
//        ["A","A","A","A","A","A"],
//        ["A","A","A","A","A","A"],
//        ["A","A","A","A","A","A"],
//        ["A","A","A","A","A","A"]]
//        "AAAAAAAAAAAAAAB"
        char[][] board = {
                {'A', 'A', 'A', 'A', 'A', 'A'},
                {'A', 'A', 'A', 'A', 'A', 'A'},
                {'A', 'A', 'A', 'A', 'A', 'A'},
                {'A', 'A', 'A', 'A', 'A', 'A'},
                {'A', 'A', 'A', 'A', 'A', 'A'},
                {'A', 'A', 'A', 'A', 'A', 'A'}
        };
        String word = "AAAAAAAAAAAAAAB";

//        [["a","b"],["c","d"]]
//        "cdba"
//        char[][] board = {
//                {'a', 'b'},
//                {'c', 'd'}
//        };
//        String word = "cdba";

        RecallPractice rp = new RecallPractice();

        AUtil.printCostInMs(() -> {
            boolean exist = rp.exist(board, word);
            log("exist = " + exist);
        }, "exist");
    }

    public void log(String msg) {
        log("Exam1", msg);
    }

    @Override
    public void log(String tag, String msg) {
        System.out.println(tag + ": " + msg);
    }
}
